package com.ruoyi.plus.config;

import com.ruoyi.framework.config.RedisConfig;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;

/**
 * 支持redis集群锁-redisson
 * @author likun
 */
public class RedissonConfig extends RedisConfig {

    @Bean
    public RedissonClient redissonClient(RedisConnectionFactory connectionFactory) {
        Config config = new Config();
        LettuceConnectionFactory factory = (LettuceConnectionFactory) connectionFactory;
        String url = "redis://" + factory.getHostName() + ":" + factory.getPort();
        config.useSingleServer().setAddress(url).setDatabase(factory.getDatabase()).setPassword(factory.getPassword());
        RedissonClient redisson = Redisson.create(config);
        return redisson;
    }
}
